package com.d3works.apps.helpdesk.db.repo;

import com.d3works.apps.base.service.app.AppBaseService;
import com.d3works.apps.helpdesk.db.Ticket;
import com.d3works.apps.helpdesk.service.TicketService;
import com.d3works.exception.D3worksException;
import com.d3works.exception.service.TraceBackService;
import com.google.inject.Inject;

public class TicketManagementRepository extends TicketRepository {

  @Inject private TicketService ticketService;
  @Inject private AppBaseService appBaseService;

  @Override
  public Ticket save(Ticket ticket) {

    try {
      ticketService.computeSeq(ticket);
    } catch (D3worksException e) {
      TraceBackService.traceExceptionFromSaveMethod(e);
    }
    ticketService.computeSLA(ticket);
    ticketService.checkSLAcompleted(ticket);
    return super.save(ticket);
  }

  @Override
  public Ticket copy(Ticket entity, boolean deep) {
    Ticket copy = super.copy(entity, deep);
    copy.setStatusSelect(null);
    copy.setProgressSelect(null);
    copy.setStartDateT(appBaseService.getTodayDateTime().toLocalDateTime());
    copy.setTicketSeq(null);
    return copy;
  }
}
